﻿using System;
using System.IO;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class EquimentFittingMark : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new NotImplementedException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				for (;;)
				{
					transaction = new Transaction(document, "设备管件标注");
					transaction.Start();
					ISelectionFilter selectionFilter = new EquipmentAccesoryFilter();
					Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择要标注的设备管件");
					Element element = document.GetElement(reference);
					XYZ pnt = selection.PickPoint("请选择标注位置的点");
					string text = "";
					int integerValue = element.Category.Id.IntegerValue;
					BuiltInCategory builtInCategory = BuiltInCategory.OST_MechanicalEquipmentTags;
					if (integerValue == -2001140)
					{
						text = "系统图机械设备标记";
						builtInCategory = BuiltInCategory.OST_MechanicalEquipmentTags;
					}
					else if (integerValue == -2001160)
					{
						text = "系统图卫浴装置标记";
						builtInCategory = BuiltInCategory.OST_PlumbingFixtureTags;
					}
					else if (integerValue == -2008055)
					{
						text = "系统图附件标记";
						builtInCategory = BuiltInCategory.OST_PipeAccessoryTags;
					}
					YJKRevitTools.DeleteTagOfElement(document, builtInCategory, element);
					FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(document, text, (Autodesk.Revit.DB.BuiltInCategory)(element.Category.Id.IntegerValue));
					if (familySymbol != null)
					{
						IndependentTag independentTag = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, pnt);
						independentTag.ChangeTypeId(new ElementId(familySymbol.Id.IntegerValue));
						independentTag.HasLeader = true;
					}
					else if (familySymbol == null)
					{
						string text2 = Path.Combine(Product.InstallLocation, "MarkTagFamily\\EquipmentTagFamily\\" + text + ".rfa");
						document.LoadFamily(text2);
						RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, pnt).HasLeader = true;
					}
					transaction.Commit();
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && 1 == (int)transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
